package co.edu.eam.iso.xml;

import java.io.File;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import co.edu.eam.iso.enums.CharacteristicEnum;
import co.edu.eam.iso.enums.ScaleEnum;
import co.edu.eam.iso.enums.SubCharacteristicEnum;
import co.edu.eam.iso.model.Characteristic;
import co.edu.eam.iso.model.Metric;
import co.edu.eam.iso.model.SubCharacteristic;
import co.edu.eam.iso.util.BigDecimalUtil;

public class XMLReader {

	private static XMLReader instance = null;

	public Map<CharacteristicEnum, Characteristic> readXML(String fileName)
			throws Exception {
		Map<CharacteristicEnum, Characteristic> characteristics = new HashMap<CharacteristicEnum, Characteristic>();
		SAXBuilder saxBuilder = new SAXBuilder();
		File xmlFile = new File(fileName);
		
		Document document = (Document) saxBuilder.build(xmlFile);
		Element rootNode = document.getRootElement();

		for (Object characteristicNode : rootNode.getChildren("caracteristica")) {
			Element characteristicElement = (Element) characteristicNode;
			
			Characteristic characteristic = new Characteristic();
			characteristic.setCharacteristicEnum(CharacteristicEnum
					.getByString(characteristicElement.getAttribute("nombre").getValue()));
			characteristic.setWeight(BigDecimalUtil.round(BigDecimalUtil.wrap(
					Double.parseDouble(characteristicElement.getAttribute("valor").getValue())), 2,
					RoundingMode.HALF_UP));
			
			Map<SubCharacteristicEnum, SubCharacteristic> subcharacteristics = new HashMap<SubCharacteristicEnum, SubCharacteristic>();
			
			for (Object subcharacteristicNode : characteristicElement.getChildren("subcaracteristica")) {
				Element subcharacteristicElement = (Element) subcharacteristicNode;
								
				SubCharacteristic subCharacteristic = new SubCharacteristic();
				subCharacteristic.setSubCharacteristicEnum(SubCharacteristicEnum
						.getByString(subcharacteristicElement.getAttribute("nombre").getValue()));
				subCharacteristic.setWeight(BigDecimalUtil.round(BigDecimalUtil.wrap(
						Double.parseDouble(subcharacteristicElement.getAttribute("valor").getValue())), 2,
						RoundingMode.HALF_UP));
								
				List<Metric> metrics = new ArrayList<Metric>();
				
				for (Object metricNode : subcharacteristicElement.getChildren("metrica")) {
					Element metricElement = (Element) metricNode;
					
					String description = metricElement.getAttribute("descripcion").getValue();
					ScaleEnum scaleEnum = ScaleEnum.getByString(metricElement.getAttribute("evaluacion").getValue());
					
					Metric metric = new Metric();
					metric.setSubCharacteristicEnum(subCharacteristic.getSubCharacteristicEnum());
					metric.setDescription(description);
					metric.setScale(scaleEnum);
					metrics.add(metric);
				}
				
				subCharacteristic.setMetrics(metrics);
				subcharacteristics.put(subCharacteristic.getSubCharacteristicEnum(), subCharacteristic);
			}
			
			characteristic.setSubcharacteristics(subcharacteristics);
			characteristics.put(characteristic.getCharacteristicEnum(), characteristic);
		}
		
		return characteristics;
	}

	public static XMLReader getInstance() {
		if (instance == null) {
			instance = new XMLReader();
		}
		return instance;
	}
}
